package com.itheima.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 短信发送工具类
 */

/**
 * 短信发送工具类
 */
public class SMSUtils {
    public static final String VALIDATE_CODE = "SMS_205439265";//发送短信验证码
    public static final String ORDER_NOTICE = "SMS_205439265";//体检预约成功通知
    public static final String REGION_ID = "cn-hangzhou";
    public static final String ACCESS_KEY_ID = "LTAI4GCoYy6nCBUPA5Y3YzCj";
    public static final String ACCESS_KEY_SECRET = "wPbNQ77aekJIRklRuboMg4Y7bi2MiJ";
    public static final String SIGN_NAME = "远点";


    public static void main(String[] args) {
        System.out.println(sendShortMessage("17679067205"));
    }

    /**
     * 发送短信并返回验证码
     */
    public static String sendShortMessage(String sendyourTelephoneNumber) {
        return sendShortMessage(VALIDATE_CODE, sendyourTelephoneNumber, ValidateCodeUtils.generateValidateCode(4).toString());
    }

    /**
     * 发送短信并返回验证码
     */
    public static String sendShortMessage(String templateCode, String sendyourTelephoneNumber, String smsCode) {

        DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", REGION_ID);
        request.putQueryParameter("PhoneNumbers", sendyourTelephoneNumber);
        request.putQueryParameter("SignName", SIGN_NAME);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + smsCode + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return smsCode;
    }

}
